<template>

  <div class="login-page">

    <van-nav-bar title="面经登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[
        { required: true, message: '请填写用户名' },
        {pattern: /^\w{3,8}$/,message: '宝贝,用户名为3~8位哦~~~'}
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[
        { required: true, message: '请填写密码' },
        {pattern: /^\w{6,}/,message: '宝贝,密码至少得6位哦~~~'}
        ]"
      />
      <div style="margin: 16px">
        <van-button color="pink" size="large" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
    <router-link class="link" to="/register">注册账号</router-link>
    <div class="picture">
      <img
        src="https://img2.baidu.com/it/u=545061015,3288863041&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=707"
        alt=""
      />
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'

export default {
  name: 'login-page',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      console.log('submit', values)
      const res = await login(values)
      this.$toast.success('登录成功')
      // console.log(res)
      setToken(res.data.token)
      setTimeout(() => {
        this.$router.push('/')
      }, 1000)
    }
  }

}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
.picture {
  width: 300px;
  height: 500px;
}
img {
  width: 100%;
}
</style>
